
local entrys = {}
for i,data in ipairs(load_excel('英雄设置.xlsx','3星词条')) do
    entrys[#entrys + 1] = data.tip
end

for i,data in ipairs(load_excel('英雄设置.xlsx','英雄天赋')) do
    local name = data.name
    data.skill_type = "passive"
    data.skill_class = '天赋'
    data.base_tip = data.tip
    data.cooling_flag = true
    data.tip = ('%s|n|n[天赋技能]|n英雄专属天赋，需要一星觉醒后激活'):format(data.tip)
    ac.lni('skill',name,data)
end


local skill_list = {}
local dmg_color = {
    ['力量'] = '|cffff0000力量*%0.f%%|r',
    ['敏捷'] = '|cff00ff00敏捷*%0.f%%|r',
    ['智力'] = '|cff00ccff智力*%0.f%%|r',
    ['全属性'] = '|cffff6800全属性*%0.f%%|r',
    ['攻击力'] = '|cffff6800攻击力*%0.f%%|r',
    ['生命上限'] = '|cffff0000生命*%0.f%%|r',
    ['生命恢复'] = '|cff00ff00生命恢复*%0.f%%|r',
}
local dmg_color2 = {
    ['适应'] = '|cffffff00自适应伤害|r',
    ['物理'] = '|cffff6800物理伤害|r',
    ['魔法'] = '|cffca42fd魔法伤害|r',
}

for i,data in ipairs(load_excel('英雄设置.xlsx','英雄技能')) do
    local name = data.name
    local tip = data.tip:gsub('([%d]+[%d%.]?[%d]-)([\0%%]?)',function(num,ex)
        return ('|cff00ff00%s%s|r'):format(num,ex)
    end)
    data.bind_temp = ac.lni('skill',data['绑定'])
    data.damage_type = data['类型']
    data.skill_class = '技能'
    data.skill_type = "passive"
    data.temp = '攻击触发'
    data.max_level = 4
    data.base_tip = tip
    data.cooling_flag = true
    data.tip = [['base_tip'|n|n'xx_tip''sj_tip']]
    data.dmg_unpack = text_unpack_dmg(data['伤害公式'])
    --data.dmg_obj = ac.str2value(data['伤害公式'])
    
    --1星文本
    data.tip1 = function(self)
        return ('1星效果：|n%s'):format(format_obj(self.bind_temp,self.bind_temp.base_tip))
    end

    data.dmg_str = function(self)
        local tips = {}
        for a,da in ipairs(self.dmg_unpack) do
            local key = da[1]
            local rate = da[2]
            local c = da[3]
            local color1 = dmg_color[key]
            if c~='%' then
                rate = rate * 100
            end
            rate = rate * self.dmg_rate
            tips[a] = color1:format(rate)
        end
        return ('(%s)的%s'):format(table.concat(tips,'+'),dmg_color2[self.damage_type])
    end
    
    data.dmg_rate = 1
    data.dmg_get = function(self)
        local hero = self.owner
        if hero==nil then
            return 0
        end
        local damage = 0
        local rate2 = self.dmg_rate or 1
        for a,da in ipairs(self.dmg_unpack) do
            local key = da[1]
            local rate = da[2]
            local c = da[3]
            if c=='%' then
                rate = rate / 100
            end
            damage = hero:get(key)*rate*rate2
        end
        return damage
    end

    --2星文本
    
    data.tip2 = data.tip2 or '2星效果：|n提高技能50%系数' 
    --3星文本
    data.tip3 = data.tip3 or '3星效果：|n随机词条' 

    data.xx_tip = function(self)
        local hero = self.owner
        local level = self.level
        -- if hero==nil then
        --     return '[英雄技能]|n英雄专属技能，最多七个技能，可使用技能吞噬丹吞噬'
        -- end
        local tip1 = format_obj(self,self.tip1)
        local tip2 = format_obj(self,self.tip2)
        local tip3 = format_obj(self,self.tip3)
        
        if level>1 then
            local tips = {tip1,tip2,tip3}
            for a,tip in ipairs(tips) do
                if level>=a+1 then
                    tips[a] = tip:gsub('([%d]+[%d%.]?[%d]-)([\0%%]?)',function(num,ex)
                        return ('|cff00ff00%s%s|r'):format(num,ex)
                    end)
                else
                    tips[a] = ('|cff999999%s|r'):format(tip)
                end
            end
            return table.concat(tips,'|n|n')
        else
            local tips = {tip1,tip2,tip3}
            return ('|cff999999%s|r'):format(table.concat(tips,'|n|n'))
        end
    end

    data.sj_tip = function(self)
        if self.level<self.max_level then
            return '|n|n|cffff0000使用对应的觉醒石提高技能星级|n3星后自动吞噬|r'
        end
        return '|n|n|cffff0000已经满星|r'
    end

    ac.lni('skill',name,data)
    skill_list[#skill_list + 1] = name
    
end

local exiocns = {
    [[图标\绿.blp]],
    [[图标\蓝.blp]],
    [[图标\紫.blp]],
}

ac.game:event '单位-等级变化'(function(_,hero,temp)
    if temp.skill_class=='技能' then
        local player = hero.owner
        local level = temp.level
        temp.exiocn = exiocns[temp.level-1]
        if temp.level==2 then
            temp:gc(hero:add_skill(temp['绑定']))
        end
        if temp.level==3 then
            temp.dmg_rate = temp.dmg_rate * 1.5
        end
        if temp.level==4 then
            local tip,index = table.random(entrys)
            temp.tip3_base = tip
            temp.tip3 = ('3星效果：|n%s'):format(tip)
            if index==1 then
                temp:gc(hero:add('杀敌智力',0.5))
            elseif index==2 then
                temp:gc(hero:add('杀敌敏捷',0.5))
            elseif index==3 then
                temp:gc(hero:add('杀敌力量',0.5))
            elseif index==4 then
                temp.dmg_rate = temp.dmg_rate * 1.5
            elseif index==5 then
                temp.hit_unit = function(_,target)
                    if target:get('当前生命百分比')<8 then
                        target:kill(hero)
                    end
                end
            elseif index==6 then
                temp:gc(hero:add('杀敌全属性',0.3))
            elseif index==7 then
                temp.cool = math.max(temp.cool - 2,0)
            elseif index==8 then
                temp.hit_unit = function(_,target)
                    target:add_buff '临时属性'{
                        time = 5,
                        sx = {
                            ['攻击加成'] = -10,
                        }
                    }
                end
            elseif index==9 then
                temp.hit_unit = function(_,_,data)
                    data.armor = data.armor * 0.9
                end
            elseif index==10 then
                temp['杀怪金币'] = 3
            elseif index==11 then
                temp['杀怪杀敌'] = 1
            elseif index==12 then
                temp:gc(hero:event '单位-攻击命中'(function()
                    temp:set_cd(temp:get_cd() - 0.1)
                end))
            elseif index==13 then
                temp.count = 3000
                temp.tip3 = ('3星效果：|n%s[剩余|cff00ff00%s|r]'):format(temp.tip3_base,temp.count)
                temp:gc(hero:event '英雄-击杀单位'(function(trg)
                    temp.count = temp.count - 1
                    if temp.count==0 then
                        trg:remove()
                        temp.tip3 = ('3星效果：|n%s|cffff0000[已经触发]|r'):format(temp.tip3_base)
                        hero:add('伤害增幅',15)
                    else
                        temp.tip3 = ('3星效果：|n%s[剩余|cff00ff00%s|r]'):format(temp.tip3_base,temp.count)
                    end
                end))
            elseif index==14 then
                temp.on_run_start = function()
                    hero:add_buff '临时属性'{
                        time = 5,
                        sx = {
                            ['暴击几率'] = 3,
                        }
                    }
                end
            elseif index==15 then
                temp.on_crit = function(_,target,data)
                    if data['天雷']==nil then
                        local damage = hero:get('攻击力') * 1.5
                        hero:aoe_damage(target,300,damage)
                    end
                end
            elseif index==16 then
                local old_run = temp.on_run
                temp.on_run = function(...)
                    old_run(...)
                    if math.random(100)<=30 then
                        old_run(...)
                    end
                end
            elseif index==17 then
                temp:gc(hero:add('冷却速度',20))
            elseif index==18 then
                temp:gc(hero:add('攻击距离',200))
            elseif index==19 then
                temp:gc(hero:add('物理伤害',15))
                temp:gc(hero:add('魔法伤害',15))
            end
            if temp['杀怪金币'] or temp['杀怪杀敌'] then
                temp:gc(hero:event '英雄-击杀单位'(function(trg)
                    player:add('金币',temp['杀怪金币'] or 0)
                    player:add('杀敌',temp['杀怪杀敌'] or 0)
                end))
            end
            player:set_data('吞噬技能',true)
            ac.sync:notify('同步-吞噬技能',player,temp.slotid)
        end
    end
end)

--[读取英雄身上随机技能的觉醒石]
function ac.unit.__index:create_jx_item()
    local list = {}
    for a=1,7 do
        local skill = self:find_skill(a,'英雄')
        if skill then
            list[#list + 1] = skill.name
        end
    end
    local name = nil
    if #list>0 then
        name = table.random(list)
    end
    local point = self:get_point()
    local item = ac.item.create('觉醒石',point,{bind_name = name})
    collate_item(self,item)
    return item
end

local function upgrade_skill(hero,name)
    local temp = hero:find_skill(name)
    if temp and temp.level < temp.max_level then
        temp:add_level(1)
        return true
    end
end

local dmg_color3 = {
    ['力量'] = '|cffff0000【力量】|r',
    ['敏捷'] = '|cff00ff00【敏捷】|r',
    ['智力'] = '|cff00ccff【智力】|r',
    ['全属性'] = '|cffff6800【全属性】|r',
    ['攻击'] = '|cffff6800【攻击力】|r',
    ['生命'] = '|cffff0000【生命】|r',
    ['恢复'] = '|cff00ff00【恢复】|r',
}
ac.item('觉醒石'){
    cool = 0.3,
    once_flag = true,
    food = 1500,
    model = [[wp_jsx.mdl]],
    show_effect = function(self)
        local player = ac.player.self
        local hero = player.hero
        local skill = hero and hero:find_skill(self.bind_name)
        return skill and skill.level<skill.max_level
    end,
    on_init = function(self)
        if self.bind_name==nil then
            self.bind_name = table.random(skill_list)
        end
        local temp = ac.lni('skill',self.bind_name)
        self.title = ('|cffff9900[觉醒石]|r%s'):format(temp.title or temp.name)
        self.art = temp.art
        self.class_tip = ('%s【%s】'):format(dmg_color3[temp['分属']],dmg_color2[temp['类型']])
        self.tip1 = format_obj(temp,temp.tip1):gsub('|n','')
        self.tip = [['class_tip'|n|n'tip1'|n2星觉醒：提高技能系数|n3星觉醒，随机技能词条|n|n|cffff0000使用对应的觉醒石提高技能星级|r]]
    end,
    on_create = function(self)
        japi.SetUnitTexture(self.handle,self.art, 0)
    end,
    on_use = function(self)
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        if upgrade_skill(hero,self.bind_name) then
            self:remove()
        else
            self:set_count(self:get_count())
            player:send_msg('该技能已满级或不存在')
        end
    end
}



ac.wait(10,function()
    local test_data = load_excel('英雄设置.xlsx','英雄技能')
    ac.game:notify('测试-注册数据','英雄技能',test_data,function(_,player,name)
        local hero = player.hero
        if hero then
            local temp = hero:find_skill(name)
            if temp and upgrade_skill(hero,name) then

            else
                hero:add_skill(name,'英雄') 
            end
        end
    end)

end)